package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.KeyStroke;

import layout.TableLayout;

import com.hexidec.util.Translatrix;

import core.UniPaukerCore;

import eventHandling.EventBus;
import eventHandling.EventTyp.Type;

/**
 * The MainFrame is the central Frame of uniPauker.
 */
public class MainFrame extends JFrame {
	/*
	 * constants
	 */
	private final int NOT_NEEDED = -1;
	// Menu
	private static final String MENU_OPEN = "Open";
	private static final String MENU_NEW_LESSON = "NewLesson";
	private static final String MENU_SAVE = "Save";
	private static final String MENU_EXIT = "Exit";
	private static final String MENU_FILE = "File";
	// Buttons
	private static final String ADD_CARD = "AddCard";
	private static final String LEARN_NOT_LEARNED_CARDS = "LearnNotLearnedCards";
	private static final String LEARN_EXPIRED_CARDS = "LearnExpiredCards";
	/*
	 * Non GUI Elements
	 */
	private StatisticsPanel statisticsPanel;
	private EventBus eventBus;
	/*
	 * GUI Elements
	 */
	private JMenuBar menuBar = new JMenuBar();
	private JMenu menuFile = new JMenu();
	private JMenuItem mItemOpen = new JMenuItem();
	private JMenuItem mItemNewLesson = new JMenuItem();
	private JMenuItem mItemSave = new JMenuItem();
	private JMenuItem mItemExit = new JMenuItem();
	private JButton btnAddCard = new JButton();
	private JButton btnLearnUnlearnedCards = new JButton();
	private JButton btnLearnExpiredCards = new JButton();

	private JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
	private JPanel pnlButtons = new JPanel();
	private JPanel pnlOverview = new JPanel();
	private JPanel pnlSouth = new JPanel();

	public MainFrame(EventBus eventBus, StatisticsPanel statisticsPanel) {
		this.eventBus = eventBus;
		this.statisticsPanel = statisticsPanel;
		this.setTitle(UniPaukerCore.APP_NAME + " - " + UniPaukerCore.VERSION);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		initBundle();
		initMenuBar();
		initSplitPane();

		this.add(splitPane);
		this.setJMenuBar(menuBar);
		this.setSize(500, 500);
		splitPane.setDividerLocation(0.5);
		splitPane.repaint();
	}

	private void initSplitPane() {
		initButtonPanel();

		statisticsPanel.setSize(100, 100);

		pnlSouth.setLayout(new BorderLayout());
		pnlSouth.add(pnlButtons, BorderLayout.WEST);
		pnlSouth.add(pnlOverview, BorderLayout.CENTER);

		splitPane.setRightComponent(pnlSouth);
		splitPane.setLeftComponent(statisticsPanel);

	}

	private void initBundle() {
		Translatrix.setBundleName("view.LanguageResources");
		Translatrix.setLocale(new Locale("german"));
	}

	private void initButtonPanel() {
		initButton(btnAddCard, ADD_CARD, Type.OPEN_ADD_CARD_DIALOG);
		initButton(btnLearnUnlearnedCards, LEARN_NOT_LEARNED_CARDS,
				Type.LEARN_UNLEARNED_CARDS);
		initButton(btnLearnExpiredCards, LEARN_EXPIRED_CARDS,
				Type.LEARN_EXPIRED_CARDS);
		btnAddCard.setEnabled(false);
		btnLearnExpiredCards.setEnabled(false);
		btnLearnUnlearnedCards.setEnabled(false);

		double border = 5;
		double size[][] = { { border, 120, border }, // Columns
				{ border, 60, 60, 60, border } }; // Rows
		pnlButtons.setLayout(new TableLayout(size));
		// col row
		pnlButtons.add(btnAddCard, "1,1");
		pnlButtons.add(btnLearnExpiredCards, "1,2");
		pnlButtons.add(btnLearnUnlearnedCards, "1,3");
	}

	private void initMenuBar() {

		initMenuItem(mItemNewLesson, MENU_NEW_LESSON, KeyEvent.VK_N,
				InputEvent.CTRL_DOWN_MASK, false, Type.OPEN_NEW_LESSON_DIALOG);
		initMenuItem(mItemOpen, MENU_OPEN, NOT_NEEDED, NOT_NEEDED, false,
				Type.OPEN);
		initMenuItem(mItemSave, MENU_SAVE, KeyEvent.VK_S,
				InputEvent.CTRL_DOWN_MASK, false, Type.SAVE);
		initMenuItem(mItemExit, MENU_EXIT, KeyEvent.VK_F4,
				InputEvent.ALT_DOWN_MASK, false, Type.EXIT);

		menuFile.setText(Translatrix.getTranslationString(MENU_FILE));
		menuFile.add(mItemNewLesson);
		menuFile.add(mItemOpen);
		menuFile.add(mItemSave);
		menuFile.add(mItemExit);

		menuBar.add(menuFile);

	}

	/**
	 * Inits a JMenuItem with the necessary data
	 * 
	 * @param mItem
	 * @param translateString
	 * @param keyCode
	 * @param modifiers
	 * @param onKeyRelease
	 * @param eventType
	 */
	private void initMenuItem(JMenuItem mItem, String translateString,
			int keyCode, int modifiers, boolean onKeyRelease,
			final Type eventType) {
		mItem.setText(Translatrix.getTranslationString(translateString));
		if (keyCode != NOT_NEEDED && modifiers != NOT_NEEDED) {
			mItem.setAccelerator(KeyStroke.getKeyStroke(keyCode, modifiers,
					onKeyRelease));
		}
		mItem.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				eventBus.fireEvent(eventType);
			}
		});
	}

	/**
	 * Helps to init all the buttons
	 * 
	 * @param btnToInit
	 * @param translateString
	 * @param eventType
	 */
	private void initButton(JButton btnToInit, String translateString,
			final Type eventType) {
		btnToInit.setText(Translatrix.getTranslationString(translateString));
		btnToInit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				eventBus.fireEvent(eventType);

			}
		});
	}

	public void enableAddCardButton() {
		btnAddCard.setEnabled(true);
	}

	public void enableLearnUnlearnedCardsButton() {
		btnLearnUnlearnedCards.setEnabled(true);
	}

	public void enableLearnExpiredCardsButton() {
		btnLearnExpiredCards.setEnabled(true);
	}

	public void disableAddCardButton() {
		btnAddCard.setEnabled(false);
	}

	public void disableLearnUnlearnedCardsButton() {
		btnLearnUnlearnedCards.setEnabled(false);
	}

	public void disableLearnExpiredCardsButton() {
		btnLearnExpiredCards.setEnabled(false);
	}

	@Override
	public void repaint() {
		splitPane.setDividerLocation(0.5);
		super.repaint();
	}

}